package com.toune.movie.mvp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.toune.movie.R;
import com.toune.movie.base.BaseActivity;
import com.toune.movie.base.myUtil.MyUtil;
import com.toune.movie.mvp.adapter.AdapterButtonHor;
import com.toune.movie.mvp.adapter.AdapterVariety;
import com.toune.movie.mvp.contract.TvVideoContract;
import com.toune.movie.mvp.presenter.TvVideoPresenter;
import com.toune.movie.vo.VarietyVo;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/5/15.
 */

public class TvVideoActivity extends BaseActivity<TvVideoPresenter> implements TvVideoContract.View {
    @BindView(R.id.mLreclView)
    RecyclerView mLreclView;
    @BindView(R.id.rclview)
    RecyclerView rclview;
    private int currentPage = 1;
    private AdapterVariety adapterVariety;
    private List<VarietyVo> allVarietyList = new ArrayList<>();
    private String url;
    private AdapterButtonHor adapterButtonHor;

    @Override // 实例化presenter
    public TvVideoPresenter initPresenter() {
        return new TvVideoPresenter();
    }

    @Override
    public void loadMore() {
        MyUtil.showProDialog(context);
        currentPage++;
        String moreUrl = url.substring(0, url.length() - 1);
        mPresenter.getVo(moreUrl + currentPage, currentPage);
    }

    @Override
    public void refreData() {
        MyUtil.showProDialog(context);
        currentPage = 1;
        allVarietyList.clear();
        mPresenter.getVo(url, currentPage);
    }

    @Override
    public void init(Bundle savedInstanceState) {
        MyUtil.showProDialog(context);
        url = getIntent().getStringExtra("url");
        mPresenter.getVo(url, currentPage);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_tv_video;
    }

    @Override
    protected String getTitleStr() {
        return getResources().getString(R.string.tv);
    }


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            notifyAdapter();
            MyUtil.hidProDialog();
        }
    };

    private void notifyAdapter() {
        if (adapterVariety == null) {
            adapterVariety = new AdapterVariety(R.layout.adapter_variety, allVarietyList);
            mLreclView.setLayoutManager(new GridLayoutManager(context, 3));
            mLreclView.setAdapter(adapterVariety);
            adapterVariety.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Intent intent = new Intent(context, VarietyDetailActivity.class);
                    intent.putExtra("url", allVarietyList.get(position).getDetailUrl());
                    intent.putExtra("title", allVarietyList.get(position).getTitle());
                    intent.putExtra("tag", 1);//0表示是电影，1表示其他
                    intent.putExtra("imgUrl", allVarietyList.get(position).getImagSrc());
                    startActivity(intent);
                }
            });
        } else {
            adapterVariety.notifyDataSetChanged();
        }
    }

    @Override
    protected void initEventAndData() {

    }

    @Override
    public void setVo(List<VarietyVo> videoList) {
        if (!allVarietyList.containsAll(videoList)) {
            allVarietyList.addAll(videoList);
            handler.sendEmptyMessage(0);
        }
    }

    @Override
    public void setHeaderVos(final List<VarietyVo> headerVos) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (adapterButtonHor == null) {
                    adapterButtonHor = new AdapterButtonHor(R.layout.layout_radio_button, headerVos);
                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
                    linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                    rclview.setLayoutManager(linearLayoutManager);
                    rclview.setAdapter(adapterButtonHor);
                    adapterButtonHor.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                            url = headerVos.get(position).getDetailUrl();
                            refreData();
                            adapterButtonHor.setSelectedPosition(position);
                        }
                    });
                } else {
                    adapterButtonHor.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void useNightMode(boolean isNight) {

    }
}
